import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteWorkbook;

import Entity.DANXUAN_Entity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

@Data
@Getter
@Setter
@EqualsAndHashCode
public class BookF {

        public static List<String> cmp_strs=Arrays.asList("多选","单选","判断","问答");
        // 工作簿名称
        public String book_name;

        // 工作簿文件路径，注意
        public File book_file;
        
        // 工作簿类型
        private ExcelTypeEnum type=ExcelTypeEnum.XLS;
        // 数据
        // @SuppressWarnings("rawtypes")
        private List<SheetF> sheet_list=new LinkedList<>();


        // 判断是否能够正常生成excel文件
        // public boolean is_youxiao=true;

        public BookF() {
        }

        public BookF(String book_name) {
            this.book_name = book_name;
        }

        public BookF(File book_file) {
            this.book_file = book_file;
            this.book_name=this.book_file.getName().split("\\.")[0];
        }

        // @SuppressWarnings("rawtypes")
        private void sort_Sheet(){
            // Collections.sort(this.sheet_list);
            // Collections.sort(this.sheet_list, new Comparator<SheetF>() {

            //     @Override
            //     public int compare(SheetF o1, SheetF o2) {
            //         return cmp_strs.indexOf(o1.getSheet_name())-cmp_strs.indexOf(o2.getSheet_name());
            //     }
                
            // });
        }

        public  HashMap<Choose_Type,SheetF> sheet_HashMap=new HashMap<>(){{
            put(Choose_Type.DANXUAN,new SheetF("单选",Choose_Type.DANXUAN));
            put(Choose_Type.DUOXUAN, new SheetF("多选",Choose_Type.DUOXUAN));
            put(Choose_Type.PANDUAN, new SheetF("判断",Choose_Type.PANDUAN));
            put(Choose_Type.WENDA, new SheetF("问答",Choose_Type.WENDA));
        }};
    
        public static HashMap<Choose_Type,List<Integer>> include_columns_HashMap=new HashMap<>(){{
            put(Choose_Type.DANXUAN,List.of(0,1,2,3,4,5,6,7,8,9));
            put(Choose_Type.DUOXUAN,List.of(0,1,2,3,4,5,6,7,8,9));
            put(Choose_Type.PANDUAN,List.of(0,1,2,3));
            put(Choose_Type.WENDA,List.of(0,1,2,3));
        }};

        // @SuppressWarnings("rawtypes")
        public void writer(){
            // 单元薄先添加
        //    this.add_sheet(this.sheet_HashMap.values());
            // 单元薄先排序
            // this.sort_Sheet();

            // 接下来处理单选、多选问题
            Deal(sheet_HashMap.get(Choose_Type.DANXUAN),sheet_HashMap.get(Choose_Type.DUOXUAN));

            // WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
            WriteWorkbook writeWorkbook=new WriteWorkbook();
            writeWorkbook.setExcelType(this.type);
            writeWorkbook.setFile(this.book_file);
            writeWorkbook.setAutoTrim(true);
            // writeWorkbook.setClazz(DANXUAN_Entity.class);

            ExcelWriter excelWriter = new ExcelWriter(writeWorkbook);
            for (String string : cmp_strs) {
                for (SheetF sheetF : this.sheet_HashMap.values()) {
                    WriteSheet writeSheet = EasyExcel.writerSheet(sheetF.getSheet_name()).build();
                    writeSheet.setClazz(DANXUAN_Entity.class);
                    writeSheet.setAutoTrim(true);
                    writeSheet.setNeedHead(true);
                    // writeSheet.
                    // writeSheet.setinc
                    // 自定义行列
                    DIY_Column(sheetF.sheet_type, writeSheet);
                    if(string.equals(sheetF.getSheet_name())){
                        excelWriter.write(data(sheetF.getData()), writeSheet);
                    }
                }
            }
            excelWriter.close();
        }


        private void DIY_Column(Choose_Type sheet_type, WriteSheet writeSheet) {
            writeSheet.setIncludeColumnIndexes(include_columns_HashMap.get(sheet_type));
        }

        private void Deal(SheetF danxuan, SheetF duoxuan) {
            List<DANXUAN_Entity> data1=new ArrayList<>();
            List<DANXUAN_Entity> data2=new ArrayList<>();
            
            for (DANXUAN_Entity one : danxuan.getData()) {
                if(one.getStr3()!=null && !one.getStr3().isEmpty() ){
                    if(one.getStr3().length()==1){
                        data1.add(one);
                    }
                }else{
                    data1.add(one);
                }
            }

            for (DANXUAN_Entity one : duoxuan.getData()) {
                // data2.add(one);
                if (one.getStr3() != null && !one.getStr3().isEmpty()) {
                    if (one.getStr3().length() == 1) {
                        data1.add(one);
                    }
                }
            }



            for (DANXUAN_Entity one : duoxuan.getData()) {
                // data2.add(one);
                if(one.getStr3()!=null && !one.getStr3().isEmpty()){
                    if(one.getStr3().length()!=1){
                        data2.add(one);
                    }
                }else{
                    data2.add(one);
                }
            }

            for (DANXUAN_Entity one : danxuan.getData()) {
                if(one.getStr3()!=null && !one.getStr3().isEmpty()){
                    if(one.getStr3().length()!=1){
                        data2.add(one);
                    }
                }
            }

            danxuan.setData(data1);
            // danxuan.da
            duoxuan.setData(data2);
        }

        @SuppressWarnings("rawtypes")
        private Collection<?> data(List data) {
            return data;
        }

        public void add_sheet(SheetF sheet) {
            this.sheet_list.add(sheet);
        }

        public <T> void add_sheet() {
            for (SheetF it : sheet_HashMap.values()) {
                this.sheet_list.add(it);
            }
        }

        public void add_sheet(Collection<SheetF> values) {
            for (SheetF sheetF : values) {
                this.sheet_list.add(sheetF);
            }
            
        }
}
